import { _decorator, EventTouch, Node, Sprite, SpriteFrame, UITransform, Vec3 } from 'cc';
import { app } from 'db://app-game/core/app';
import { ViewUtil } from 'db://app-game/core/utils/ViewUtil';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { CCComp } from 'db://app-game/module/common/CCComp';
import { Joysitick } from '../Joysitick';
const { ccclass } = _decorator;

@ccclass('JoysitickManagerComp')
@ecs.register('JoysitickManager', false)
export class JoysitickManagerComp extends CCComp {
  joysitick: Joysitick = null!;

  private body: Node;
  private stick: Node;
  private base: Node;
  private outerRadius: number = 0;

  protected onLoad() {
    this.joysitick = this.ent as Joysitick;
    this.body = this.node.getChildByName('body');
    this.base = this.body.getChildByName('base');
    this.stick = this.body.getChildByName('stick');
  }

  protected start(): void {
    this.refreshSvg();
    this.init();
  }

  refreshSvg() {
    const { width, height, absolutePosition } = this.joysitick.JoysitickModel;
    const { baseSvg, stickSvg } = this.joysitick.JoysitickModel;
    // 1. 设置摇杆精灵图片
    this.base.getComponent(Sprite).spriteFrame = app.res.get(baseSvg, SpriteFrame);
    this.stick.getComponent(Sprite).spriteFrame = app.res.get(stickSvg, SpriteFrame);
    // 2.设置摇杆大小
    this.node.getComponent(UITransform).width = width;
    this.node.getComponent(UITransform).height = height;
    this.body.getComponent(UITransform).width = width;
    this.body.getComponent(UITransform).height = height;

    // 3.设置摇杆位置
    ViewUtil.AddWidgetComponent(this.node, absolutePosition);
  }

  init() {
    this.outerRadius = this.body.getComponent(UITransform).contentSize.x / 2;
    this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
    this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  }
  onDestroy() {
    this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
    this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  }

  /** 摇杆移动开始 */
  onTouchStart(e: EventTouch) {
    const touchPos = e.getUILocation();
  }

  /** 摇杆移动中 */
  onTouchMove(e: EventTouch) {
    if (this.joysitick.JoysitickControl.disable) return;
    const uiPos = e.getUILocation();
    const touchPos = this.body
      .getComponent(UITransform)
      .convertToNodeSpaceAR(new Vec3(uiPos.x, uiPos.y, 0));
    const stickPos = new Vec3(
      touchPos.x - this.body.position.x,
      touchPos.y - this.body.position.y,
      0
    );
    if (stickPos.length() > this.outerRadius) {
      stickPos.multiplyScalar(this.outerRadius / stickPos.length());
    }
    this.stick.setPosition(stickPos.x, stickPos.y);

    this.joysitick.JoysitickControl.input = stickPos.clone().normalize();
    this.joysitick.JoysitickControl.stickLength = stickPos.length();
  }

  /** 摇杆移动结束 */
  onTouchEnd() {
    this.stick.setPosition(0, 0);
    this.joysitick.JoysitickControl.input = Vec3.ZERO;
  }

  reset(entity: ecs.Entity): void {
    this.joysitick.JoysitickControl.input = Vec3.ZERO;
    this.node.destroy();
  }
}
